<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="css/loginRegister.css"/>
    <title>iBOOK-注册</title>
</head>
<body id="registerPage">
<form>
    <p>
        <input type="text" id="user_name" name="user_name" placeholder="用户名" />
    </p>
    <p>
        <input type="password" id="pswd" name="pswd" placeholder="密码" maxlength="6"/>
    </p>
    <p>
        <input type="password" id="pswd2" name="pswd2" placeholder="确认密码" maxlength="6"/>
    </p>
    <p>
        <input type="button" value="注册" id="register"/>
    </p>
    <p>
        <a href="login.html" id="left">已有账号</a>
        <span id="right">
				<a href="index.html">网站首页</a>
			</span>
    </p>
</form>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    window.onload = function (){
        document.getElementById('register').onclick = function (){
            let name = document.querySelector("#user_name").value.trim(); // ✅ 去除首尾空格
            let pswd = document.querySelector("#pswd").value;
            let pswd2 = document.querySelector("#pswd2").value;

            if(!name || !pswd || !pswd2){
                alert("用户名和密码不能为空");
                return;
            }

            if (pswd !== pswd2) { // ✅ 校验两次密码是否一致
                alert("两次密码输入不一致");
                return;
            }

            if (pswd.length < 6 || pswd2.length < 6) { // ✅ 密码长度≥6位
                alert("密码长度不能少于6位");
                return;
            }



            axios({
                method:'post',
                url:'/dem1/UserController/register',
                data:{
                    user_name:name,
                    pswd:pswd,
                    pswd2:pswd2
                }
            }).then(response =>{
                let obj = response.data;
                console.log("后端响应",obj);
                if(obj.success){
                    location.href = "/dem1/login.html";
                }else {
                    alert(obj.message || "注册失败")
                }
            }).catch(error =>{
                console.error("注册失败：",error);
                alert("网络异常，请重试")
            })


        }
    }
</script>



</body>